package com.ruoyi.utils;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.baomidou.mybatisplus.extension.service.IService;
import com.ruoyi.dj.service.*;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

@Component
public class DbServiceUtil {

    private static SwdataFutureSolarF107Service solarF107Service;
    private static SwdataFutureSolarY107Service solarY107Service;
    private static SwdataFutureSolarS107Service solarS107Service;
    private static SwdataFutureSolarM107Service solarM107Service;
    private static SwdataFutureMagApService magApService;
    private static SwdataFutureMagKpService magKpService;
    private static SwdataFutureMagDstService magDstService;


    //太阳辐射指数预报 和 地磁指数预报 数据保存
    public static void savePreData(String index,
                             String time,
                             String method,
                             List<String> timeListOrigin,
                             List<Object> predListOrigin) {

        int num = timeListOrigin.size()/2;

        List<String> timeList = timeListOrigin.subList(timeListOrigin.size() - num, timeListOrigin.size());
        List<Object> predList = predListOrigin.subList(predListOrigin.size() - num, predListOrigin.size());

        IService service = findService(index);
        //通过service执行sql先删除已经存在的，然后直接新增即可
        QueryWrapper<Model> queryWrapper = new QueryWrapper<Model>();
        queryWrapper.eq("\"PRE_TIME\"", time);
        queryWrapper.eq("\"METHOD\"", method);
        List<Model> list = service.list(queryWrapper);
        if (CollUtil.isNotEmpty(list)) {
            service.removeBatchByIds(list);
        }
        Class<? extends Model> entityClass = service.getEntityClass(); //后面的流式计算是需要先表明这个class是基于Object的，否则cast异常
        List<Model> saveList = IntStream.range(0, timeList.size())
                .mapToObj(i -> {
                    //移除 预报为空的值
                    if (predList.get(i).equals("null")) {
                        return null;
                    }
                    Map<String, String> map = MapUtil.builder(new HashMap<String, String>())
                            .put("preTime", time)
                            .put("datetime", timeList.get(i))
                            .put("method", method)
                            .put("value",String.valueOf(predList.get(i)))
                            .build();
                    return BeanUtil.mapToBean(map, entityClass, true, null);
                }).collect(Collectors.toList());
        List<Model> collect = saveList.stream().filter(a -> a != null).collect(Collectors.toList());
        service.saveBatch(collect);
    }

    private static IService findService(String index){
        IService service = null;
        switch (index){
            case "F107":
                service = solarF107Service;
                break;
            case "S107":
                service = solarS107Service;
                break;
            case "M107":
                service = solarM107Service;
                break;
            case "Y107":
                service = solarY107Service;
                break;
            case "Ap":
                service = magApService;
                break;
            case "Kp":
                service = magKpService;
                break;
            case "Dst":
                service = magDstService;
                break;
        }
        return service;
    }

    public DbServiceUtil(SwdataFutureSolarF107Service solarF107Service, SwdataFutureSolarY107Service solarY107Service, SwdataFutureSolarS107Service solarS107Service, SwdataFutureSolarM107Service solarM107Service, SwdataFutureMagApService magApService, SwdataFutureMagKpService magKpService, SwdataFutureMagDstService magDstService) {
        DbServiceUtil.solarF107Service = solarF107Service;
        DbServiceUtil.solarY107Service = solarY107Service;
        DbServiceUtil.solarS107Service = solarS107Service;
        DbServiceUtil.solarM107Service = solarM107Service;
        DbServiceUtil.magApService = magApService;
        DbServiceUtil.magKpService = magKpService;
        DbServiceUtil.magDstService = magDstService;
    }

}
